
/**************************/
/** WORKER - CSS problem **/
/**************************/

double focW_E(const double xval, void * params){
    
    struct paramsgolden *focparams= (struct paramsgolden *) params;
    
    double *expW_WWE_JE          = focparams->expW_WWE_JE_ST;
    double *expW_WWNE_JNE        = focparams->expW_WWNE_JNE_ST;
    double *expW_WWE             = focparams->expW_WWE_ST;
    double *expW_WWNE            = focparams->expW_WWNE_ST;
    
    double *expU_UiNE_JNE        = focparams->expU_UiNE_JNE_ST;
    double *expU_UiNE            = focparams->expU_UiNE_ST;
    double *expU_UiE_JE          = focparams->expU_UiE_JE_ST;
    double *expU_UiE             = focparams->expU_UiE_ST;
#if OPT_compute_C_flow == 1
    double *C_expW_WWE_JE          = focparams->C_expW_WWE_JE_ST;
    double *C_expW_WWNE_JNE        = focparams->C_expW_WWNE_JNE_ST;
    double *C_expW_WWE             = focparams->C_expW_WWE_ST;
    double *C_expW_WWNE            = focparams->C_expW_WWNE_ST;
    
    double *C_expU_UiNE_JNE        = focparams->C_expU_UiNE_JNE_ST;
    double *C_expU_UiNE            = focparams->C_expU_UiNE_ST;
    double *C_expU_UiE_JE          = focparams->C_expU_UiE_JE_ST;
    double *C_expU_UiE             = focparams->C_expU_UiE_ST;
#endif
#if SEA == 1
    double *expU_UiE_JEi         = focparams->expU_UiE_JEi_ST;
    double *expU_UiNE_JNEi       = focparams->expU_UiNE_JNEi_ST;
#if OPT_compute_C_flow == 1
    double *C_expU_UiE_JEi         = focparams->C_expU_UiE_JEi_ST;
    double *C_expU_UiNE_JNEi       = focparams->C_expU_UiNE_JNEi_ST;
#endif
#endif
    
    int igridfoc=focparams->igridST;    int tgridfoc=focparams->tgridST;    int ygridfoc=focparams->ygridST;
    int excluded=focparams->excludedST;
    
    double cons,xgrid,dxgrid,valfoc;
    double diffValue, diffgrid, ddiffgrid, searcheffJ;
    double tempWWNE_JNE, tempWWNE, tempWWE_JE, tempWWE, tempUiNE_JNE, tempUiNE, tempUiE_JE, tempUiE, tempUiNE_JNEi, tempUiE_JEi;
    double C_tempWWNE_JNE, C_tempWWNE, C_tempWWE_JE, C_tempWWE, C_tempUiNE_JNE, C_tempUiNE, C_tempUiE_JE, C_tempUiE, C_tempUiNE_JNEi, C_tempUiE_JEi;
    int idiffgrid, ixgrid, inxW, inxW1, inxU, inxU1;
    
    // CONSUMPTION
    cons    = findconsW(xval, igridfoc, tgridfoc, ygridfoc);
    xgrid   = invexpspace(xval,Gridmin,Gridmax,Echelle1,maxgrid);
    dxgrid  = weightinter(xval, xgrid, &ixgrid, grid, maxgrid);
    if ((xval>(Gridmax))){printf("focWW: (xval>(Gridmax)) %20.15f\n",xval);getchar();}
    if ((ixgrid>=(maxgrid-1))){printf("focWW: ixgrid>=(maxgrid-1) %d\n",ixgrid);getchar();}
    if ((cons<=0.0)){printf("focWW: consoFOC<0.0 %20.15f\t%20.15f\t%20.15f\n",xval,cons, findconsW(0.0, igridfoc, tgridfoc, ygridfoc));getchar();}
    if ((xval<(0.0))){printf("focWW: (xval<(0.0)) %20.15f\n",xval);getchar();}
    
    
    // CONTINUATION VALUE //
    inxW  = inxW(ixgrid,tgridfoc,ygridfoc);
    inxW1 = inxW((ixgrid+1),tgridfoc,ygridfoc);
    inxU  = inx(ixgrid,tgridfoc);
    inxU1 = inx((ixgrid+1),tgridfoc);
    
    tempWWNE_JNE    = inter1d(dxgrid,expW_WWNE_JNE[inxW],expW_WWNE_JNE[inxW1]);
    tempWWNE        = inter1d(dxgrid,expW_WWNE[inxW],expW_WWNE[inxW1]);
    tempWWE_JE      = inter1d(dxgrid,expW_WWE_JE[inxW],expW_WWE_JE[inxW1]);
    tempWWE         = inter1d(dxgrid,expW_WWE[inxW],expW_WWE[inxW1]);
    
    tempUiNE_JNE    = inter1d(dxgrid,expU_UiNE_JNE[inxU],expU_UiNE_JNE[inxU1]);
    tempUiNE        = inter1d(dxgrid,expU_UiNE[inxU],expU_UiNE[inxU1]);
    tempUiE_JE      = inter1d(dxgrid,expU_UiE_JE[inxU],expU_UiE_JE[inxU1]);
    tempUiE         = inter1d(dxgrid,expU_UiE[inxU],expU_UiE[inxU1]);
#if OPT_compute_C_flow == 1
    C_tempWWNE_JNE    = inter1d(dxgrid,C_expW_WWNE_JNE[inxW],C_expW_WWNE_JNE[inxW1]);
    C_tempWWNE        = inter1d(dxgrid,C_expW_WWNE[inxW],C_expW_WWNE[inxW1]);
    C_tempWWE_JE      = inter1d(dxgrid,C_expW_WWE_JE[inxW],C_expW_WWE_JE[inxW1]);
    C_tempWWE         = inter1d(dxgrid,C_expW_WWE[inxW],C_expW_WWE[inxW1]);
    
    C_tempUiNE_JNE    = inter1d(dxgrid,C_expU_UiNE_JNE[inxU],C_expU_UiNE_JNE[inxU1]);
    C_tempUiNE        = inter1d(dxgrid,C_expU_UiNE[inxU],C_expU_UiNE[inxU1]);
    C_tempUiE_JE      = inter1d(dxgrid,C_expU_UiE_JE[inxU],C_expU_UiE_JE[inxU1]);
    C_tempUiE         = inter1d(dxgrid,C_expU_UiE[inxU],C_expU_UiE[inxU1]);
#endif
    // FIND THE OPTIMAL EFFORT LEVEL
#if NOPOLICY == 1
    if(excluded == 0) {
        diffValue = max(0.000000000001,(betapar*((1.0-etapar[tgridfoc])*(tempWWNE_JNE - tempWWNE) + (etapar[tgridfoc])*(tempUiNE_JNE - tempUiNE))));
    }
    if(excluded == 1) {
        diffValue = max(0.000000000001,(betapar*((1.0 - phipar)*((1.0-etapar[tgridfoc])*(tempWWE_JE - tempWWE) + (etapar[tgridfoc])*(tempUiE_JE - tempUiE)) + phipar*((1.0-etapar[tgridfoc])*(tempWWNE_JNE - tempWWNE) + (etapar[tgridfoc])*(tempUiNE_JNE - tempUiNE)))));
    }
#endif
    
#if SEA == 1
    tempUiNE_JNEi    = inter1d(dxgrid,expU_UiNE_JNEi[inxU],expU_UiNE_JNEi[inxU1]);
    tempUiE_JEi      = inter1d(dxgrid,expU_UiE_JEi[inxU],expU_UiE_JEi[inxU1]);
    
    if(excluded == 0) {
        diffValue = max(0.000000000001,(betapar*((1.0-etapar[tgridfoc])*(tempWWNE_JNE - tempWWNE) + (etapar[tgridfoc])*(tempUiNE_JNEi - tempUiNE))));
    }
    if(excluded == 1) {
        diffValue = max(0.000000000001,(betapar*((1.0 - phipar)*((1.0-etapar[tgridfoc])*(tempWWE_JE - tempWWE) + (etapar[tgridfoc])*(tempUiE_JEi - tempUiE)) + phipar*((1.0-etapar[tgridfoc])*(tempWWNE_JNE - tempWWNE) + (etapar[tgridfoc])*(tempUiNE_JNEi - tempUiNE)))));
    }
#endif
    
    
    // ON THE GRID OF DIFF VALUE //
    diffgrid    =   invexpspace(diffValue,gridminsearch,gridmaxsearch,EchelleW,maxsearch);
    ddiffgrid   =   weightinter(diffValue, diffgrid, &idiffgrid, diffvalues, maxsearch);
    searcheffJ  =   inter1d(ddiffgrid,search1D_E[idiffgrid],search1D_E[idiffgrid+1]);
    focparams   ->  searchJoutST=searcheffJ;
    
    
    // CONTINUATION VALUE //
#if NOPOLICY == 1
    if(excluded == 0) {
        valfoc = utilc(cons,0) + disutilityE(searcheffJ) + betapar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWNE_JNE + (etapar[tgridfoc])*(tempUiNE_JNE))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWNE + (etapar[tgridfoc])*(tempUiNE)));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,0) + betapar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWNE_JNE + (etapar[tgridfoc])*(C_tempUiNE_JNE))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWNE + (etapar[tgridfoc])*(C_tempUiNE)));
#endif
    }
    if(excluded == 1) {
        valfoc = utilc(cons,0) + disutilityE(searcheffJ) + betapar*((1.0-phipar)*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWE_JE + (etapar[tgridfoc])*(tempUiE_JE))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWE + (etapar[tgridfoc])*(tempUiE))) + phipar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWNE_JNE + (etapar[tgridfoc])*(tempUiNE_JNE))  + (1.0-piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWNE + (etapar[tgridfoc])*(tempUiNE))));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,0) + betapar*((1.0-phipar)*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWE_JE + (etapar[tgridfoc])*(C_tempUiE_JE))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWE + (etapar[tgridfoc])*(C_tempUiE))) + phipar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWNE_JNE + (etapar[tgridfoc])*(C_tempUiNE_JNE))  + (1.0-piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWNE + (etapar[tgridfoc])*(C_tempUiNE))));
#endif
    }
#endif
    
#if SEA == 1
    if(excluded == 0) {
        valfoc = utilc(cons,0) + disutilityE(searcheffJ) + betapar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWNE_JNE + (etapar[tgridfoc])*(tempUiNE_JNEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWNE + (etapar[tgridfoc])*(tempUiNE)));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,0) + betapar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWNE_JNE + (etapar[tgridfoc])*(C_tempUiNE_JNEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWNE + (etapar[tgridfoc])*(C_tempUiNE)));
#endif
    }
    if(excluded == 1) {
        valfoc = utilc(cons,0) + disutilityE(searcheffJ) + betapar*((1.0-phipar)*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWE_JE + (etapar[tgridfoc])*(tempUiE_JEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWE + (etapar[tgridfoc])*(tempUiE))) + phipar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*tempWWNE_JNE + (etapar[tgridfoc])*(tempUiNE_JNEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*tempWWNE + (etapar[tgridfoc])*(tempUiNE))));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,0) + betapar*((1.0-phipar)*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWE_JE + (etapar[tgridfoc])*(C_tempUiE_JEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWE + (etapar[tgridfoc])*(C_tempUiE))) + phipar*(piE(searcheffJ)*((1.0-etapar[tgridfoc])*C_tempWWNE_JNE + (etapar[tgridfoc])*(C_tempUiNE_JNEi))  + (1.0 - piE(searcheffJ))*((1.0-etapar[tgridfoc])*C_tempWWNE + (etapar[tgridfoc])*(C_tempUiNE))));
#endif
    }
#endif
    
    
    valfoc=-valfoc;
    return valfoc;
}

